﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using QuanLyThuPhiTaiLieu.Entity;
using QuanLyThuPhiTaiLieu.Model;

namespace QuanLyThuPhiTaiLieu.Biz
{
    public class KhaiThacVaSuDungTaiLieuBiz : BaseBiz
    {
        public int SaveItem(KhaiThacVaSuDungTaiLieu item)
        {
            var dbContext = new Context();

            dbContext.KhaiThacVaSuDungTaiLieus.Add(item);
            int newId = dbContext.SaveChanges();

            return newId;
        }
        
        public void UpdateItem(KhaiThacVaSuDungTaiLieu item)
        {
            var dbContext = new Context();
            dbContext.Entry(item).State = EntityState.Modified;
            dbContext.SaveChanges();
        }

        public List<KhaiThacVaSuDungTaiLieu> ViewAll()
        {
            var dbContext = new Context();
            return (from item in dbContext.KhaiThacVaSuDungTaiLieus
                    orderby item.TenLoaiTaiLieu ascending
                    select item).ToList();
        }

        public KhaiThacVaSuDungTaiLieu LoadItem(int id)
        {
            var dbContext = new Context();
            return (from item in dbContext.KhaiThacVaSuDungTaiLieus
                    where item.Id == id
                    select item).First();
        }

        public KhaiThacVaSuDungTaiLieu LoadItem(string ma)
        {
            var dbContext = new Context();
            var items = (from item in dbContext.KhaiThacVaSuDungTaiLieus
                    where item.MaTaiLieu == ma
                    select item).ToList();

            if (items.Count > 0)
            {
                return items.First();
            }

            return null;
        }

        public List<KhaiThacVaSuDungTaiLieu> LoadItemByMa(string ma)
        {
            var dbContext = new Context();
            return (from item in dbContext.KhaiThacVaSuDungTaiLieus
                         where item.MaTaiLieu.IndexOf(ma) == 0
                         select item).ToList();           
        }

        public void RemoveItem(List<KhaiThacVaSuDungTaiLieu> items)
        {
            var dbContext = new Context();
            foreach (var tailieu in items)
            {
                dbContext.KhaiThacVaSuDungTaiLieus.Attach(tailieu);
                dbContext.KhaiThacVaSuDungTaiLieus.Remove(tailieu);
                dbContext.Entry(tailieu).State = EntityState.Deleted;
            }

            dbContext.SaveChanges();
        }

        public List<string> GetAllLevel1()
        {
            var dbContext = new Context();

            return (from item in dbContext.KhaiThacVaSuDungTaiLieus
                                 select item.LoaiTaiLieu).Distinct().ToList();                
        }

        public List<string> GetAllLevel2()
        {
            var dbContext = new Context();

            return (from item in dbContext.KhaiThacVaSuDungTaiLieus
                    select item.DangLoaiTaiLieu).Distinct().ToList();
        }

        public List<string> GetAllTenTaiLieu()
        {
            var dbContext = new Context();

            return (from item in dbContext.KhaiThacVaSuDungTaiLieus
                    select item.TenLoaiTaiLieu).Distinct().ToList();
        }

        public List<string> GetAllMaTaiLieu()
        {
            var dbContext = new Context();

            return (from item in dbContext.KhaiThacVaSuDungTaiLieus
                    select item.MaTaiLieu).Distinct().ToList();
        }

        public List<KhaiThacVaSuDungTaiLieu> GetItemByLevel1(string level1)
        {
            var dbContext = new Context();

            return (from item in dbContext.KhaiThacVaSuDungTaiLieus
                    where item.LoaiTaiLieu == level1
                    select item).ToList();
        }

        public List<KhaiThacVaSuDungTaiLieu> GetItemByLevel2(string level1, string level2)
        {
            var dbContext = new Context();

            return (from item in dbContext.KhaiThacVaSuDungTaiLieus
                    where item.LoaiTaiLieu == level1 && item.DangLoaiTaiLieu == level2
                    select item).ToList();
        }

        public List<string> GetAllLevel2ByLevel1(string level1)
        {
            var dbContext = new Context();

            return (from item in dbContext.KhaiThacVaSuDungTaiLieus
                    where item.LoaiTaiLieu == level1
                    select item.DangLoaiTaiLieu).Distinct().ToList();
        }

        public List<KhaiThacVaSuDungTaiLieu> SearchByName(string name)
        {
            var dbContext = new Context();

            return (from item in dbContext.KhaiThacVaSuDungTaiLieus
                    where item.TenLoaiTaiLieu.ToLower().Contains(name.ToLower())
                    orderby item.TenLoaiTaiLieu
                    select item).ToList();
        }

        public List<KhaiThacVaSuDungTaiLieu> SearchByName(string type, string name)
        {
            var dbContext = new Context();

            return (from item in dbContext.KhaiThacVaSuDungTaiLieus
                    where item.LoaiTaiLieu.ToLower().Contains(name.ToLower()) && 
                    string.Equals(item.TenLoaiTaiLieu,name, System.StringComparison.InvariantCultureIgnoreCase)
                    orderby item.TenLoaiTaiLieu
                    select item).ToList();
        }
    }
}
